<?php

namespace App\Http\Controllers\Admin\Setting;

use App\B2BProject\Admin\AdminControllerBuildTrait;
use App\B2BProject\Admin\AdminSetting;
use App\B2BProject\AttachmentPrivateFiles;
use App\B2BProject\Custom\SiteSetting;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class SiteSettingController extends Controller
{
    use AdminControllerBuildTrait;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $setting = SiteSetting::findOrFail(1);

        return view('admin/setting/site-setting/index')->with('setting', $setting);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // TODO 文件上传路径可能要调整
        $setting      = SiteSetting::findOrFail(1);
        $data['name'] = $request->input('name');
        if($request->file('logo')){
            $file         = $request->file('logo');
            $new = $this->saveWebLogo($file, $setting);
            $data['logo'] =$new->id;
        }


        /*if ($request->hasFile('member-logo')) {
            $file = $request->file('member-logo');

            $fileName_member_logo = Str::random('8') . '.' . $file->getClientOriginalExtension();

            $file->move(base_path('storage/app/admin/upload'), $fileName_member_logo);
            $data['memberLogo'] = $fileName_member_logo;
        }
        if ($request->hasFile('business-logo')) {
            $file = $request->file('business-logo');

            $fileName_business_logo = Str::random('8') . '.' . $file->getClientOriginalExtension();

            $file->move(base_path('storage/app/admin/upload'), $fileName_business_logo);
            $data['businessLogo'] = $fileName_business_logo;
        }*/

        $setting->update($data);

        return redirect()->route('admin.site-setting.index');
    }

    /**
     * 保存网站图片
     *
     * @param $file
     * @return mixed
     * @throws FileException
     */
    public function saveWebLogo($file, $setting)
    {
        if ($file->isValid()) {

            $fileName_web_log = Str::random('8') . '.' . $file->getClientOriginalExtension();
            $sha512           = hash_file('sha512', $file->getPathname());
            $extension        = $file->guessClientExtension();
            $localName        = md5($sha512) . '_' . time() . '.' . $extension;
            $mime             = $file->getMimeType();
            $origin           = $file->getClientOriginalName();
            $size             = $file->getSize();

            $target = trim(str_replace(base_path(), '',
                $file->move(base_path('storage/app/admin/upload'), $fileName_web_log)), '\/');

            if ($setting->logo) {
                $new = AttachmentPrivateFiles::findOrFail($setting->logo);
            } else {
                $new = new AttachmentPrivateFiles();
            }

            $new->sha512        = $sha512;
            $new->mime_type     = $mime;
            $new->local_path    = $target;
            $new->extension     = $extension;
            $new->original_name = $origin;
            $new->size          = $size;
           $setting->logo ? $new->update() : $new->save();
            return $new;
        } else {
            throw new FileException('Upload error');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
